<%@ page language="java" import="com.alibaba.fastjson.JSONObject" pageEncoding="UTF-8" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="java.io.*" %>
<%@ page import="java.nio.file.Files" %>
<%--流程退回调用QMS接口--%>
<%
    JSONObject jsonData = new JSONObject();
    BaseBean baseBean = new BaseBean();
    String realPath = request.getRealPath("/");
    baseBean.writeLog(">>>>>uploadfile.jsp>>>>>realPath=" + realPath);
    String path = Util.null2String(request.getParameter("path"));
    path = path.substring(0, path.lastIndexOf("/") + 1);
    baseBean.writeLog(">>>>>uploadfile.jsp>>>>>path=" + path);
    String status = "";
    String message = "";
    //boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    //baseBean.writeLog(">>>>>uploadfile.jsp>>>>>isMultipart=" + isMultipart);
    //if (isMultipart) {
        fileUpload(request, path, realPath);
   // } else {
        status = "error";
        message = "Request is not multipart content";
   // }
    jsonData.put("status", status);
    jsonData.put("message", message);
    out.print(jsonData.toJSONString());
%>
<%!

    private static void fileUpload(HttpServletRequest request, String path, String realPath) {
        BaseBean baseBean = new BaseBean();
        try {
            byte[] byteArray = {65, 66, 67, 68, 69}; // 这是一个包含"A", "B", "C", "D", "E"字符的字节数组

            // 将字节数组转换为InputStream
            InputStream inputStream = new ByteArrayInputStream(byteArray);
//            FileItemFactory factory = new DiskFileItemFactory();
//            ServletFileUpload upload = new ServletFileUpload(factory);
//            List<FileItem> multiparts = upload.parseRequest(request);
//            for (FileItem item : multiparts) {
//                baseBean.writeLog(">>>>>uploadfile.jsp>>>>>item=" + item);
//                if (!item.isFormField()) {
////                    String name = item.getName();
////                    baseBean.writeLog(">>>>>uploadfile.jsp>>>>>name=" + name);
////                    String fileSeparator = File.separator;
////                    baseBean.writeLog(">>>>>uploadfile.jsp>>>>>fileSeparator=" + fileSeparator);
////                    if (fileSeparator.equals("\\")) {
////                        path = path.replace("/", "\\");
////                    }
////                    baseBean.writeLog(">>>>>uploadfile.jsp>>>>>path=" + path);
////                    ensureDirectoryExists(realPath + path);
////                    String filePath = realPath + path + name;
////                    baseBean.writeLog(">>>>>uploadfile.jsp>>>>>filePath=" + filePath);
//                    // inputStream = item.getInputStream();
//                    // 写入文件
//                    writeInputStreamToFile(inputStream);
//
//                    baseBean.writeLog(">>>>>uploadfile.jsp>>>>>upload success");
//                }
//            }
            writeInputStreamToFile(inputStream);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void ensureDirectoryExists(String directoryPath) {
        File directory = new File(directoryPath);

        // 判断目录是否存在
        if (!directory.exists()) {
            // 目录不存在，尝试创建
            if (!directory.mkdirs()) {
                throw new RuntimeException("Failed to create directory: " + directoryPath);
            }
        } else if (!directory.isDirectory()) {
            throw new IllegalArgumentException("Specified path is not a directory: " + directoryPath);
        }

        // 确保目录可写，以防后续操作因权限问题失败
        if (!Files.isWritable(directory.toPath())) {
            throw new IllegalStateException("Directory is not writable: " + directoryPath);
        }
    }

    private static void writeInputStreamToFile(InputStream inputStream) throws IOException {
        BaseBean baseBean = new BaseBean();
        try {
            String outputPath = "d:\\WEAVER\\ecology\\interface\\zhlx\\demo\\getPDFdwr_Ajax.jsp";
            FileOutputStream fileOutputStream = new FileOutputStream(outputPath);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            byte[] buffer = new byte[8192];
            int bytesRead = 0;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                baseBean.writeLog("bytesRead: " + bytesRead);
                baseBean.writeLog("buffer: " + buffer);
                bufferedOutputStream.write(buffer, 0, bytesRead);
            }
        } catch (Exception e) {
            System.err.println("An error occurred while processing the input stream: " + e.getMessage());
        }
    }


%>


